package astar;

import java.util.Arrays;
import static astar.AStarConstants.*;

public class Area {

    public int width, height;
	
	private int[][] terrain;
	
	public Area(int width, int height) {
		terrain = new int[width][height];
		for(int[] row:terrain) {
			Arrays.fill(row, TERRAIN_PASSABLE);
		}
		this.width = width;
		this.height = height;
	}
	
	public void edit(int x, int y, int terrainType) {
		this.terrain[x][y] = terrainType;
	}
	
	public int getTerrainAt(int x, int y) {
		return this.terrain[x][y];
	}
	
	
	public String toString(Path path) {
		char[] areaString = toString().toCharArray();

		for(int y=0; y<this.height; y++) {
			int x = 0;
			for(x=0; x<this.width; x++) {
				for(int i=0; i<path.nodes.length; i++) {
					Node pathNode = path.nodes[i];
					if(pathNode.x == x && pathNode.y == y) {
						areaString[x+(y*this.width)+y] = PATH_CHAR;
					}
				}
			}
			areaString[x+(y*this.width)+y]='\n';
		}

		return new String(areaString);
	}

	public String toString() {
		int strLength = (this.height * (this.width+1));
		char[] str = new char[strLength];

		int x = 0, y = 0, pos=0;
		for(y=0; y<this.height; y++) {
			for(x=0; x<this.width; x++) {
				if(this.terrain[x][y]==TERRAIN_PASSABLE) {
					str[pos] = TERRAIN_PASSABLE_CHAR;
				} else if(this.terrain[x][y]==TERRAIN_IMPASSABLE) {
					str[pos] = TERRAIN_IMPASSABLE_CHAR;
				} else if(this.terrain[x][y]==TERRAIN_SOME_WHAT_PASSABLE) {
					str[pos] = TERRAIN_SOME_WHAT_PASSABLE_CHAR;
				}
				pos = pos+1;
			}
			str[pos]='\n';
			pos=pos + 1;
		}

		return new String(str);
	}
	
}
